<?php
# ============================================================================
# 版权所有  Chilcoo.com，并保留所有权利。
# 网站地址: http://www.chilcoo.com/erphp
# ----------------------------------------------------------------------------
# 这不是一个自由软件！你不能通过任何方式破解本软件.
# 使用:不允许对程序代码以任何形式任何目的的再发布。
# ============================================================================
# $Author: Chilcoo Inc. *
# $Official website : www.chilcoo.com
# 
# 文件信息:
# File : /fact/default.php
# 厂家列表页面

# 载入常用集
require_once (dirname(__FILE__).'/../../include/common.inc.php');
require_once (dirname(__FILE__).'/../../include/_manager/login.inc.php');
# 页面级别
$conf_web_page_level = 4;

class page_index  extends page{
	function __construct(){
		$this->database = new db(config::conf_db_host,config::conf_db_user,
			config::conf_db_password,config::conf_db_name);
	}
	function len(){
		$l = 0;
		try{
			$sql = "SELECT name FROM cs2_fact;";
			$l = $this->database->count($sql);
		}catch (Exception $e) {
			$l = -1;
		}
		return $l;
	}
	function listview(){
		$lv;
		try{
			$sql = "
				SELECT factId , cs2_fact.name, note, addr, tel, contact, 
				bank, branch, bankno,qq,email,info,cs2_fact.lang,
				cs2_langset.fontset,cs2_langset.charset
				FROM cs2_fact LEFT JOIN cs2_langset
				ON cs2_langset.lang = cs2_fact.lang
				ORDER BY name DESC;";
			$lv = $this->database->select($sql);
		}catch (Exception $e) {
			$lv = null;
		}
		return $lv;
	}
	function ajax_callback(){
		# POST 数据
		$token = $_POST['token'];
		# 初始返回用 json
		$json = array("ref"=>"error");
		if($token == config::TOKEN_UP_FACT){
			$name = $_POST['name'];
			$factId = $_POST['factId'];
			$contact = $_POST['contact'];
			$tel = $_POST['tel'];
			$addr = $_POST['addr'];
			$bank = $_POST['bank'];
			$branch = $_POST['branch'];
			$bankno = $_POST['bankno'];
			$qq = $_POST['qq'];
			$email = $_POST['email'];
			$info = $_POST['info'];
			$lang = $_POST['lang'];
			
			$sql = "UPDATE cs2_fact SET 
				name='".$name."' ,
				contact='".$contact."' ,
				tel='".$tel."' ,
				addr='".$addr."' ,
				bank='".$bank."' ,
				branch='".$branch."' ,
				bankno='".$bankno."' ,
				qq='".$qq."' ,
				info='".$info."' ,
				lang='".$lang."' ,
				email='".$email."' 
				WHERE (factId =". $factId.");";
			try {
				$this->database->update($sql);
				$json = array("ref"=>"OK","name"=>$name,"factId"=>$factId);
			} catch (Exception $e) {
				$json = array("ref"=>"error,database.");
			}
		}
		if($token == config::TOKEN_DEL_FACT){
			$factId = $_POST['factId'];
			$sql = "DELETE FROM cs2_fact WHERE (cs2_fact.factId=".$factId.")";
			try {
				$this->database->delete($sql);
				$json = array("ref"=>"OK","name");
			} catch (Exception $e) {
				$json = array("ref"=>"error,database.");
			}
		}
		if($token == config::TOKEN_ADD_FACT){
			$name = $_POST['name'];
			$contact = $_POST['contact'];
			$tel = $_POST['tel'];
			$addr = $_POST['addr'];
			$bank = $_POST['bank'];
			$branch = $_POST['branch'];
			$bankno = $_POST['bankno'];
			$qq = $_POST['qq'];
			$email = $_POST['email'];
			$info = $_POST['info'];
			$lang = $_POST['lang'];
			
			$sql = "INSERT INTO cs2_fact (name, note,contact,tel,addr,
				bank,branch,bankno,qq,info,lang,email) 
			VALUES (
				'".$name."',
				'',
				'".$contact."',
				'".$tel."',
				'".$addr."',
				'".$bank."',
				'".$branch."',
				'".$bankno."',
				'".$qq."',
				'".$info."',
				'".$lang."',
				'".$email."');";
			try {
				$this->database->insert($sql);
				$json = array("ref"=>"OK");
			} catch (Exception $e) {
				$json = array("ref"=>"error,database.");
			}
		}
		return json_encode($json);
	}
}

$pg = new page_index();

# Session 和 POST 安全访问.
if($_SERVER['REQUEST_METHOD'] != 'POST'){
	$_SESSION[config::conf_session_id_token] = $config->web_page_period;
	$config->smarty->assign('len', $pg->len());
	$config->smarty->assign('listview', $pg->listview());
	$pg->render($config->temp_page_path.config::conf_web_template_extension);
}else{
	print_r($pg->ajax_callback());
}
?>






